<?php
namespace app\admin\controller;
use app\admin\model\Qr as Qrmodel;

class Qr extends Base
{
    /***
     * 加好友的二维码列表
     * @return \think\response\View
     */
    public function index()
    {
        $qr = D('qr');
        $list = $qr->where('enable',0)->order('is_select','desc')->select();
        
        foreach ($list as &$tmp){
            $tmp['qr'] = config('httpurl').($tmp['qr']);
        }
        unset($tmp);
        $this->assign('list',$list);
        return view();
    }

    /**
     * 添加二维码
     */
    public function addQr()
    {
        $data = input();
        $qr = D('Qr');
        $files = request()->file('img');
		var_dump($files);
        $info = $files->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads');
        if($info) {
            $path = $info->getSaveName();
            $pathinfo = DS . 'static' . DS .'uploads' . DS . $path;
            $tmp['qr'] = str_replace('\\','/',$pathinfo);
        }
        $tmp['name'] = $data['ncontent'];
        //判断是否已经存在二维码，如果不存在，当前置顶
        $rel = $qr->where('enable',0)->find();
        if(empty($rel)) {
            $tmp['is_select'] = 1;
        }
        $list = $qr->insert($tmp);
        if($list) {
            $this->redirect('Qr/index');
        }
    }

    /**
     * 删除二维码
     */
    public function delQr()
    {
        $data = input();
        $qr = D('qr');
        $list = $qr->where('id',$data['id'])->update(['enable'=>1]);
        if($list) {
            $this->redirect('Qr/index');
        }
    }

    /**
     * 二维码置顶
     */
    public function setTop()
    {
        $data = input();
        //清除已置顶项，将当前项设为置顶项
        $qr = D('qr');
        $qr->where('enable',0)->where('is_select',1)->update(['is_select'=>0]);
        $list = $qr->where('id',$data['id'])->update(['is_select'=>1]);
        if($list) {
            $this->redirect('Qr/index');
        }
    }
}
